package com.jwx.service.Impl;

import com.jwx.mapper.UserMapper;
import com.jwx.pojo.User;
import com.jwx.service.UserService;
import com.jwx.utils.Md5Util;
import com.jwx.utils.ThreadLocalUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;

@Service

public class UserServiceImpl  implements UserService {
//在UserService要调用下层的方法，需要依赖注入
    @Resource
    private UserMapper userMapper;

    @Override
    public User findUserByName(String username) {
        User user = userMapper.selectUserByName(username);
        return user;
    }

    @Override
    public void register(String username, String password) {
        String md5String = Md5Util.getMD5String(password);//调用密码加密工具
        userMapper.insert(username,password);

    }

    @Override
    public int register01(String username, String password) {
        String md5String = Md5Util.getMD5String(password);
       int i= userMapper.insert01(username,md5String);
        return i;
    }
//业务层要完成相应的业务逻辑
    @Override
    public void update(User user) {
        user.setUpdataTtime(LocalDateTime.now());
        userMapper.update(user);
    }

    @Override
    public void updateAvatar( String avatarUrl) {
//        在mapper层中需要写更新头像的sql语句，where id=#{id}
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id =(Integer) map.get("id");
        userMapper.updateAvatar(avatarUrl,id);
    }

    @Override
    public void updatePwd(String newPwd) {
        //在Mapper层需要写更新密码的sql语句，
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        userMapper.updatePwd(Md5Util.getMD5String(newPwd),id);
    }
}
